# -*- coding: utf-8 -*-
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve, roc_auc_score
import matplotlib.pyplot as plt

def test():
    X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

    clf = LogisticRegression()  # 创建逻辑回归分类器对象
    clf.fit(X_train, y_train)  # 利用训练集 X_train 和 y_train 进行模型训练
    y_score = clf.predict_proba(X_test)[:, 1]  # 使用训练好的模型对测试集 X_test 进行预测，并提取预测为正例的概率

    fpr, tpr, thresholds = roc_curve(y_test, y_score)

    auc_score = roc_auc_score(y_test, y_score)

    plt.plot(fpr, tpr, label=f'AUC = {auc_score:.2f}')  # 绘制ROC曲线，标注AUC的值
    # 随即分类器没有分类能力，其FPR=TPR。随机分类器的性能通常表示为ROC曲线上的对角线
    plt.plot([0, 1], [0, 1], linestyle='--', color='r', label='Random Classifier')  # 绘制随机分类器的ROC曲线
    plt.xlabel('False Positive Rate')  # x轴标签为FPR
    plt.ylabel('True Positive Rate')  # y轴标签为TPR
    plt.title('ROC Curve')  # 设置标题
    plt.legend()
    plt.show()

    pass

if __name__ == '__main__':
    test()
    pass

